package com.yizisu.playerlibrary.view.video_view.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import com.yizisu.playerlibrary.IYzsPlayer
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerConfig
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerHelper

/**
 * 倍速调节文本
 */
open class PlayerErrorView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(
    context,
    attrs,
    defStyleAttr
), IPlayerHelper {


    override fun onAttachPlayerConfig(playerConfig: IPlayerConfig) {
        super.onAttachPlayerConfig(playerConfig)
        setOnClickListener {
            playerConfig.player?.retry()
            visibility = View.GONE
        }
    }

    override fun isNeedPlayerListener(): Boolean {
        return true
    }

    override fun onError(throwable: Throwable, playerModel: IYzsPlayer.Model?) {
        super.onError(throwable, playerModel)
        visibility = View.VISIBLE
        text = "${throwable.message}\n\n点击重试"
    }

    override fun onPlayerModelChange(lastModel: IYzsPlayer.Model?, playerModel: IYzsPlayer.Model) {
        super.onPlayerModelChange(lastModel, playerModel)
        visibility = View.INVISIBLE
    }
}